<template>
  <el-dialog
    :append-to-body="true"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="globalData.title"
    :visible.sync="isDialog.visible"
    top="5vh"
    width="900px"
  >
    <div class="header-box"></div>

    <div class="flex-content">
      <el-table
        v-loading="isLoading.table"
        :data="dataList"
        style="width: 100%"
      >
        <el-table-column align="center" label="名称" prop="name">
        </el-table-column>

        <el-table-column align="center" label="排序" prop="sort">
          <template slot="header">
            <el-tooltip content="越小越靠前" effect="dark" placement="top">
              <button>
                排序
                <i class="el-icon-warning-outline"></i>
              </button>
            </el-tooltip>
          </template>
        </el-table-column>

        <el-table-column align="center" label="二级类目">
          <template v-slot="{ row }">
            <el-button size="mini" type="primary" @click="handleCategory(row)">
              {{ row.juniorCategory.length }}
            </el-button>
          </template>
        </el-table-column>

        <el-table-column align="center" label="是否显示">
          <template v-slot="{ row }">
            <el-button
              v-if="row.status.origin === 1"
              size="mini"
              type="success"
              @click="handleState(row)"
            >
              {{ row.status.trans }}
            </el-button>
            <el-button
              v-if="row.status.origin === 0"
              size="mini"
              type="danger"
              @click="handleState(row)"
            >
              {{ row.status.trans }}
            </el-button>
          </template>
        </el-table-column>

        <el-table-column align="center" fixed="right" label="操作" width="150">
          <template v-slot="{ row }">
            <el-button size="mini" type="danger" @click="handleDel(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 商家分类-->
    <CategoryInfo
      v-if="isDialog.category"
      :info="dataInfo"
      :provider_id="globalData.provider_id"
      @close="closeDialog"
    ></CategoryInfo>
  </el-dialog>
</template>

<script>
import { del, lists as getList, state } from "@/api/providerCategory";
import CategoryInfo from "./categoryInfo";

export default {
  name: "Category",
  props: {
    info: {
      type: Object,
    },
  },
  components: {
    CategoryInfo,
  },
  data() {
    return {
      queryInfo: {
        provider_id: this.info.provider_id,
        parent_id: 0,
      },
      // 列表
      dataList: [],
      // 详情
      dataInfo: {},
      // 数据
      globalData: {
        title: "",
        provider_id: "",
      },
      // 加载
      isLoading: {
        table: false,
      },
      // 防抖
      queryId: +new Date(),
      // 弹窗
      isDialog: {
        visible: true,
        category: false,
      },
    };
  },
  created() {
    this.globalData.title = "商品分类" + "(" + this.info.name + ")";
    this.getData();
  },
  methods: {
    // 数据
    getData() {
      this.isLoading.table = true;
      this.queryId++;
      let queryId = this.queryId;
      getList(this.queryInfo)
        .then((res) => {
          if (queryId !== this.queryId) {
            return;
          }
          this.dataList = res.data.list;
          this.globalData.provider_id = res.data.provider_id;
        })
        .finally(() => {
          this.isLoading.table = false;
        });
    },
    // 商家分类
    handleCategory(row) {
      this.dataInfo = row;
      this.isDialog.category = true;
    },
    // 状态
    handleState(row) {
      this.$confirm("是否要修改状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== "confirm") {
            this.$message({
              type: "info",
              message: "已取消操作",
            });
            close();
            return false;
          } else {
            ctx.confirmButtonLoading = true;
            state({
              cate_id: row.cate_id,
            })
              .then(() => {
                this.getData();
                this.$message({
                  type: "success",
                  message: "操作成功",
                });
                close();
              })
              .catch(() => {})
              .finally(() => {
                ctx.confirmButtonLoading = false;
              });
          }
        },
      });
    },
    // 删除
    handleDel(row) {
      this.$confirm("此操作将删除信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== "confirm") {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
            close();
            return false;
          } else {
            ctx.confirmButtonLoading = true;
            del({
              cate_id: row.cate_id,
            })
              .then(() => {
                this.getData();
                close();
              })
              .finally(() => {
                ctx.confirmButtonLoading = false;
              });
          }
        },
      });
    },
    // 重置页面数据
    handleResetPage() {
      this.queryInfo.page = 1;
      this.getData();
    },
    //关闭
    closeDialog(val) {
      if (!val) {
        this.getData();
      }
      this.isDialog.category = false;
    },
    // 关闭
    handleClose() {
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
.el-table {
  min-height: 60vh;
}

::v-deep .el-dialog__body {
  padding: 20px 20px;
}

.header-box {
  display: flex;

  > div {
    margin-right: 10px;
  }
}

.flex-content {
  margin-top: 10px;
}
</style>
